package com.todo.event.activity.application.backend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.TodoCommonUtils;
import com.todo.event.activity.application.backend.service.EventDecisionBackendService;
import com.todo.event.activity.application.convert.EventActivityConvert;
import com.todo.event.activity.application.entity.bo.EventDecisionBO;
import com.todo.event.activity.application.entity.condition.EventDecisionConditionBO;
import com.todo.event.activity.application.service.EventDecisionService;
import com.todo.event.activity.iface.backend.request.*;
import com.todo.event.activity.iface.backend.response.EventDecisionBaseResp;
import com.todo.event.activity.iface.backend.response.EventDecisionDetailResp;
import com.todo.event.activity.common.enums.EventDecisionStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2025/1/11 21:26
 * @description
 */
@Slf4j
@Service
public class EventDecisionBackendServiceImpl implements EventDecisionBackendService {

    @Resource
    private EventDecisionService eventDecisionService;


    @Override
    public BizResponse<Boolean> insertEventDecision(InsertEventDecisionReq req) {
        EventDecisionBO eventDecisionBO = EventActivityConvert.convertToEventDecisionBO(req);
        Boolean inserted = eventDecisionService.insertEventDecision(eventDecisionBO);
        log.info("EventDecisionBackendService#insertEventDecision 新增决策成功，入参：【{}】，结果：【{}】", JSONObject.toJSONString(req), inserted);
        return BizResponse.success(inserted);
    }

    @Override
    public BizResponse<Boolean> updateEventDecision(UpdateEventDecisionReq req) {
        EventDecisionBO eventDecisionBO = EventActivityConvert.convertToEventDecisionBO(req);
        Boolean updated = eventDecisionService.updateEventDecision(eventDecisionBO);
        log.info("EventDecisionBackendService#updateEventDecision 更新事件决策成功，入参：【{}】，结果：【{}】", JSONObject.toJSONString(req), updated);
        return BizResponse.success(updated);
    }

    @Override
    public BizResponse<Boolean> removeEventDecisionById(RemoveEventDecisionByIdReq req) {
        EventDecisionBO eventDecisionBO = eventDecisionService.queryEventDecisionById(req.getDecisionId());
        if (eventDecisionBO == null){
            log.info("EventDecisionBackendService#removeEventDecisionById 删除事件决策完成，数据不存在，入参：【{}】", JSONObject.toJSONString(req));
            return BizResponse.success();
        }
        if (!eventDecisionBO.getCreateUser().equals(req.get_staff().getUsername())){
            log.error("ContentInfoService#updateContentInfo 删除事件决策失败，当前用户无权限操作 eventDecisionBO：【{}】", JSONObject.toJSONString(eventDecisionBO));
            throw new BizException(BizErrorCodeEnum.ILLEGAL_OPERATION, "当前用户无权限操作");
        }
        Boolean removed = eventDecisionService.removeEventDecision(req.getDecisionId());
        log.info("EventDecisionBackendService#removeEventDecisionById 删除事件决策成功，入参：【{}】，结果：【{}】", JSONObject.toJSONString(req), removed);
        return BizResponse.success(removed);
    }

    @Override
    public BizResponse<EventDecisionDetailResp> queryEventDecisionById(QueryEventDecisionDetailByIdReq req) {
        EventDecisionBO eventDecisionBO = eventDecisionService.queryEventDecisionById(req.getDecisionId());
        log.info("EventDecisionBackendService#queryEventDecisionById 查询事件决策详情成功，入参：【{}】，结果：【{}】", JSONObject.toJSONString(req), JSONObject.toJSONString(eventDecisionBO));
        return BizResponse.success(EventActivityConvert.convertToEventDecisionDetailResp(eventDecisionBO));
    }

    @Override
    public BizPageResponse<EventDecisionBaseResp> queryEventDecisionPage(QueryEventDecisionPageReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        EventDecisionConditionBO conditionBO = EventActivityConvert.convertToEventDecisionConditionBO(req);
        PageResult<EventDecisionBO> pageResult = eventDecisionService.queryEventDecisionPageByCondition(conditionBO);
        return BizPageResponse.success(EventActivityConvert.convertToEventDecisionBaseRespPage(pageResult));
    }

    @Override
    public BizResponse<String> testEventDecisionProperty(TestEventDecisionPropertyReq req) {
        EventDecisionBO eventDecisionBO = eventDecisionService.queryEventDecisionById(req.getId());
        if (eventDecisionBO == null) {
            log.error("EventDecisionBackendService#testEventDecisionProperty 查询事件决策详情失败，数据不存在，入参：【{}】", JSONObject.toJSONString(req));
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR, "数据不存在");
        }
        if (!eventDecisionBO.getStatus().equals(EventDecisionStatusEnum.DRAFT)) {
            log.error("EventDecisionBackendService#testEventDecisionProperty 查询事件决策详情失败，当前状态不允许操作，入参：【{}】", JSONObject.toJSONString(req));
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR, "当前状态不允许操作");
        }
        Boolean updated = eventDecisionService.updateEventDecisionStatus(req.getId(), EventDecisionStatusEnum.DRAFT, EventDecisionStatusEnum.AUDIT_SUCCESS);
        if (!updated) {
            log.error("EventDecisionBackendService#testEventDecisionProperty 更新事件决策状态失败，入参：【{}】", JSONObject.toJSONString(req));
            throw new BizException(BizErrorCodeEnum.OPERATION_FAILED);
        }
        return new BizResponse<>("ok");
    }

    @Override
    public BizResponse<Boolean> enableEventDecision(EnableEventDecisionReq req) {
        EventDecisionBO eventDecisionBO = eventDecisionService.queryEventDecisionById(req.getId());
        if (eventDecisionBO == null) {
            log.error("EventDecisionBackendService#enableEventDecision 查询事件决策详情失败，数据不存在，入参：【{}】", JSONObject.toJSONString(req));
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR, "数据不存在");
        }
        if (req.getEnable() && eventDecisionBO.getStatus().equals(EventDecisionStatusEnum.RUNNING)) {
            return BizResponse.success(true);
        }
        if (req.getEnable()){
            if (eventDecisionBO.getStatus().equals(EventDecisionStatusEnum.DRAFT)){
                log.error("EventDecisionBackendService#enableEventDecision 当前状态不允许操作，入参：【{}】", JSONObject.toJSONString(req));
                throw new BizException(BizErrorCodeEnum.PARAM_ERROR, "当前状态不允许操作");
            }
        }
        if (!req.getEnable() && eventDecisionBO.getStatus().equals(EventDecisionStatusEnum.SHUTDOWN)) {
            return BizResponse.success(true);
        }
        Boolean updated = eventDecisionService.updateEventDecisionStatus(req.getId(), eventDecisionBO.getStatus(), req.getEnable() ? EventDecisionStatusEnum.RUNNING : EventDecisionStatusEnum.SHUTDOWN);
        return BizResponse.success(updated);
    }
}
